﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SCRUM.Core.BusinessObjects;
using SCRUM.Core.DAO;
using System.Collections.Specialized;   // string dictionary

/// <summary>
/// This widget allows user to manipulate with any Task.
/// User can: filter tasks, create/edit/delete task.
/// </summary>
public partial class TaskWidget : DataOverviewWidgetWithDetails<Task,BLI>
{
    #region [ BASE ]

    /// <summary>
    /// Gets the name of the widget. It must be exactly the same as the folder that contains the widget.
    /// </summary>
    /// <value>TaskWidget</value>
    public override string Name
    {
        get { return "TaskWidget"; }
    }

    /// <summary>
    /// Gets the title. Title is shown in the header of the widget container.
    /// </summary>
    /// <value>The title.</value>
    public override string Title
    {
        get { return (string)GetLocalResourceObject("Title"); }
    }
    #endregion

    #region [ OVERRIDEN ]

    /// <summary>
    /// This method works as a substitute for Page_Load. You should use this method for
    /// data binding etc. instead of Page_Load.
    /// </summary>
    public override void LoadWidget()
    {
        ApplySettings();
        if (!IsPostBack)
        {
            RefreshWidget();
        }
    }

    /// <summary>
    /// Tasks after usage opf filters
    /// </summary>
    protected override IList<Task> itemsAcordingToFilter
    {        
        get
        {
            int taskID = (int)this.FilterID.Value == 0 ? -1 : (int)this.FilterID.Value;
            int bliID = this.Blis.SelectedBliID;
            int userStoryID = this.Blis.SelectedUserStoryID;
            int sprintID = this.Blis.SelectedSprintID;
            int releaseID = this.Blis.SelectedReleaseID;
            int projectID = this.Blis.SelectedProjectID;
            string taskName = String.IsNullOrEmpty(this.FilterName.Text) ? null : this.FilterName.Text;
            int stateID = this.FilterState.SelectedStateID;
            int userID = this.CheckBoxMyTasks.Checked ? DAOUser.GetLoggedUser().ID : -1;
            bool unassigned = this.CheckBoxUnassignedTasks.Checked;
            return DAOTask.GetAllTasks(projectID, releaseID, sprintID, userStoryID, bliID, taskID, userID, stateID, taskName, unassigned);
           
        }
    }
    #endregion

    #region [ SETTINGS ]

    private const int taskColumnOffset = 1;

    /// <summary>
    /// Gets a value indicating whether this instance is editable.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is editable; otherwise, <c>false</c>.
    /// </value>
    public override bool IsEditable
    {
        get { return true; }
    }

    /// <summary>
    /// Shows the edit.
    /// </summary>
    public override void ShowEdit()
    {
        base.ShowEdit();
        this.EditTaskWidget.Visible = true;
    }

    /// <summary>
    /// Hides the edit.
    /// </summary>
    public override void HideEdit()
    {
        base.HideEdit();
        this.EditTaskWidget.Visible = false;
    }

    /// <summary>
    /// Occurs when user clicks on Save Settings of PrototypeWidget
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SaveSettingsClick(object sender, EventArgs e)
    {
        SaveSettings();
        this.HideEdit();
        Response.Redirect(Request.Url.ToString());
        //LoadWidget();
    }
    #endregion

    #region [ ITEM MANIPULATION ]
    protected override void CreateItem()
    {
        TaskDetail.CreateItem();
        ShowOperationResult(MessageType.Info, "TaskCreated");
    }

    protected override void SaveItem()
    {
        TaskDetail.EditItem(selectedItem.ID);
        ShowOperationResult(MessageType.Info, "TaskUpdated");
    }

    protected override void DeleteItem(int itemID)
    {
        DAOTask.DeleteTask(itemID);
        ShowOperationResult(MessageType.Info, "TaskDeleted");
    }
    #endregion

    #region[ SPECIAL ]
    /// <summary>
    /// Handles the Click event of the ButtonAssignUsers control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ButtonAssignUsers_Click(object sender, EventArgs e)
    {
        Task task = TaskDetail.CreateItem();
        HideDetails();
        //loadTasks();
        //SelectTask(task);

        //ButtonAssignUsers.Visible = false;
        //ButtonCreateItem.Visible = false;
    }

    /// <summary>
    /// Refreshes whole state of the widget.
    /// </summary>
    public override void RefreshWidget()
    {
        base.RefreshWidget();

        //comments
        if (selectedItem!=null)
        {
            Comments.EntityID = selectedItem.ID;
            Comments.TypeOfEntity = "Task";
            TaskDetail.Visible = true;
        }
        Comments.Visible = selectedItem != null;
        if (Comments.Visible)
        {
            Comments.Refresh();
        }
    }

    #endregion

    #region [inherited controls mapping]
    // DataOverviewWidget
    protected override TextBox textBoxPageSize { get { return this.TextBoxPageSize; } }
    protected override Panel availableColumnsPanel { get { return this.AvailableColumns; } }
    protected override GridView data { get { return this.GridViewData; } }
    protected override int alwaysVisibleColumn { get { return taskColumnOffset; } }
    // DataOverviewWidgetWithDetails
    protected override Button buttonCreate { get { return this.ButtonCreate; } }
    protected override Button buttonCreateItem { get { return this.ButtonCreateItem; } }
    protected override Button buttonSaveItem { get { return this.ButtonSaveItem; } }
    protected override Button buttonCancelItem { get { return this.ButtonCancelItem; } }
    protected override Button buttonDeleteItem { get { return this.ButtonDeleteItem; } }
    protected override HierarchicalObjectDetails<Task,BLI> itemDetails { get { return this.TaskDetail; } }
    protected override int filtersSuperiorItemID { get { return this.Blis.SelectedBliID; } }
    protected override int filtersProjectID { get { return this.Blis.SelectedProjectID; } }
    #endregion

	protected override void GridViewData_SelectedIndexChanged(object sender, EventArgs e)
	{
		base.GridViewData_SelectedIndexChanged(sender, e);
		this.TaskDetail.DetailedTask = this.selectedItem;
	}
}
